#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# ======================================================
# @File:      : test42
# @Author     : forward_huan
# @Date       : 2024/11/16 20:56
# @Desc       :
# ======================================================
from typing import List


class Solution:
    def trap(self, height: List[int]) -> int:
        left, right = 0, len(height) - 1
        max_h, result = 0, 0
        while left < right:
            left_h, right_h = height[left], height[right]
            if left_h < right_h:
                max_h, left = max(max_h, left_h), left + 1
                if max_h > left_h:
                    result += max_h - left_h
            else:
                max_h, right = max(max_h, right_h), right - 1
                if max_h > right_h:
                    result += max_h - right_h
        return result


print(Solution().trap(height=[0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]))
print(Solution().trap(height=[4, 2, 0, 3, 2, 5]))
